#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: mutliple_inheritance.py
# Description: 
#   設計類別 (class) 時，父類別 (superclass) 可以有多個，這是說子類別 (subclass)
#   能夠繼承 (inherit) 多個父類別，使子類別可以有多種特性
# Edit History: 
#   2011-08-12    File created.
#===============================================================================
class Demo(object):
    __x = 0
 
    def __init__(self, i):
        self.__i = i
        Demo.__x += 1
     
    def __str__(self):
        return str(self.__i)
          
    def hello(self):
        print("[Demo]hello " + self.__str__())
     
    @classmethod
    def getX(cls):
        return cls.__x
 
class Other(object):
    def __init__(self, k):
        self.k = k
 
    def __str__(self):
        return str(self.k)
 
    def hello(self):
        print("[Other]hello")
     
    def bye(self):
        print("[Other]bye", self.__str__())
 
class SubDemo(Demo, Other):
    """
    當子類別繼承 (inheritance) 超過一個來源的時候，會以寫在最左邊的父類別優先繼承，
    這是說，多個父類別如果有相同名稱的屬性 (attribute) 與方法 (method) ，
    例如 __init__() 、 __str__() 等，就會以最左邊的父類別優先
    """
    def __init__(self, i, j):
        super(SubDemo, self).__init__(i)
        self.__j = j
     
    def __str__(self):
        return super(SubDemo, self).__str__() + "+" + str(self.__j)

# Demo 與 Other 有三個相同名稱的方法， __init__() 、 __str__() 與 hello() ，
# 這裡 SubDemo 改寫了 __init__() 及 __str__() ，注意 super() 是呼叫 (call) Demo 的版本，
# 而非 Other ，所以 SubDemo 的 hello() 也是 Demo 的版本。
# 至於 Demo 無 bye() ，因此 SubDemo 繼承 Other 的 bye() ，至於 bye() 當呼呼叫的 __str__() ，這則是 SubDemo 改寫後的版本 
a = SubDemo(12, 34)
a.hello()
a.bye()
b = SubDemo(56, 78)
b.hello()
b.bye()
